package com.carlinfo.common.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class EncryptUtil
{
	private Logger logger = LogManager.getLogger();
	
	private RegexUtil regexUtil = new RegexUtil();
	
	private String split = "$";
	
	private String alth = "sha256";
	
	public String encodeStr(String source)
	{
		String salt = regexUtil.proccedStr(32, "0");
		String encodeStr = DigestUtils.sha256Hex(salt+source);
		return alth+split+salt+split+encodeStr;
	}
	
	public boolean check(String source,String sourceEncodeStr)
	{
		String[] sp = sourceEncodeStr.split("\\$");
		String resStr = "";
		if(sp.length!=3)
		{
			return false;
		}
		String salt = sp[1];
		String encodeStr = DigestUtils.sha256Hex(salt+source);
		resStr = alth+split+salt+split+encodeStr;
		System.out.println(resStr);
		return resStr.equalsIgnoreCase(sourceEncodeStr);
	}
	
	public static void main(String[] args)
	{
		EncryptUtil encryptUtil = new EncryptUtil();
		String str = encryptUtil.encodeStr("123456");
		System.out.println(str);
		System.out.println(encryptUtil.check("123456", str));
		
	}
}
